<template>
  <div>
    <el-card class="box-card">

      <el-form :inline="true" :model="queryForm" class="demo-form-inline">
        <el-form-item label="域">
          <el-select
            filterable
            clearable
            @change="domainChange"
            v-model="queryForm.domainCode"
            placeholder="请选择域">
            <el-option
              v-for="item in domainOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="场景">
          <el-select
            filterable
            clearable
            :disabled="queryForm.domainCode === ''"
            v-model="queryForm.sceneCode"
            placeholder="请选择域">
            <el-option
              v-for="item in sceneOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="策略名称">
          <el-input v-model="queryForm.strategyName" placeholder="不区分大小模糊查询"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="query">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" @click="addNew">新增</el-button>
        </el-form-item>
      </el-form>

      <el-table
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="pageResult.data"
        border
        :default-sort="{prop: 'updateTime', order: 'descending'}"
        style="width: 100%; margin-top: 20px;font-size: 10px">
        <el-table-column
          align="center"
          prop="domainName"
          label="域名称">
          <template slot-scope="scope">
            {{ getDomainName(scope.row.domainCode) }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="domainCode"
          label="域Code">
        </el-table-column>
        <el-table-column
          align="center"
          label="场景">
          <template slot-scope="scope">
            {{ getSceneName(scope.row.sceneCode) }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="sceneCode"
          label="场景Code">
        </el-table-column>
        <el-table-column
          align="center"
          prop="strategyName"
          label="策略名称">
        </el-table-column>
        <el-table-column
          align="center"
          prop="desc"
          label="描述">
        </el-table-column>
        <el-table-column
          align="center"
          label="是否有效">
          <template slot-scope="scope">
            <span v-if="scope.row.isDelete === 1">无效</span>
            <span v-if="scope.row.isDelete === 0">有效</span>
          </template>
        </el-table-column>
        <el-table-column
          sortable
          align="center"
          width="300"
          label="创建/修改时间">
          <template slot-scope="scope">
            {{ recDateFormat(scope.row.createTime) }} / {{ recDateFormat(scope.row.updateTime) }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          align="center"
          width="200"
          label="操作">
          <template slot-scope="scope">
            <el-button style="margin-left: 0px" type="text" size="mini" @click="toEdit(scope.row)">编辑</el-button>
            <el-button style="margin-left: 0px;color: #189c1a" type="text" size="mini" @click="toDisableOrEnable(scope.row)">
              <span v-if="scope.row.isDelete === 1">启用</span>
              <span v-if="scope.row.isDelete === 0">禁用</span>
            </el-button>
            <!-- TODO 暂时不支持删除 -->
            <!--            <el-button style="margin-left: 0px;color: red" type="text" size="mini" @click="deleteOne(scope.row)">删除</el-button>-->
            <vue-log module="REC_STRATEGY" :target-id="scope.row.uuid"></vue-log>
          </template>
        </el-table-column>
      </el-table>

      <el-footer>
        <vue-page :paginator="pageResult.paginator" :query="query"></vue-page>
      </el-footer>
    </el-card>
    <!-- 组件们 -->
    <strategy-form ref="strategyForm"></strategy-form>
  </div>
</template>
<script>

import strategyForm from "./dialog/strategyForm";
import vuePage from "../common/vue-page.vue"
import vueLog from "../common/vue-log.vue"
import domainScene from "./dialog/domainScene";
import {strategyPageQuery} from "../../api/strategyApi";
import {loadDomainSceneSelector, loadDomainSelector} from "../../api/loadSelectorApi";

export default {
  name: "strategy",
  filters: {},
  components: {
    strategyForm,
    vuePage,
    vueLog,
    domainScene
  },
  data() {
    return {
      queryForm: {
        domainCode: '',
        sceneCode: '',
        strategyName: ''
      },
      // 查询返回结果
      pageResult: {
        data: [],
        paginator: {
          currentPage: 0,
          totalCount: 0,
          pageSize: 10,
          totalPage: 0
        }
      },
      logTargetId: undefined,
      domainOptions: [],
      sceneOptions: [],
    }
  },
  created() {
    this.query()
  },
  mounted() {
  },
  methods: {
    async query() {
      let vm = this
      this.domainOptions = await loadDomainSelector()
      let ret = await strategyPageQuery(vm.queryForm, vm.pageResult.paginator)
      if (ret.ok) {
        vm.pageResult = ret.data.queryPage
      } else {
        vm.$message({type: 'error', message: '查询失败：' + ret.message});
      }
    },
    async domainChange(value) {
      if (value) {
        this.sceneOptions = await loadDomainSceneSelector(value)
      } else {
        this.queryForm.sceneCode = ''
      }
    },
    addNew() {
      this.$refs.strategyForm.load();
    },
    toEdit(row) {
      console.log(row)
      this.$refs.strategyForm.load(row);
    },
    getDomainName(domainCode) {
      let vm = this
      for (let i = 0; i < vm.domainOptions.length; i++) {
        let domainOption = vm.domainOptions[i];
        if (domainOption.value === domainCode) {
          return domainOption.label
        }
      }
      return domainCode;
    },
    getSceneName(sceneCode) {
      let vm = this
      for (let i = 0; i < vm.sceneOptions.length; i++) {
        let sceneOption = vm.sceneOptions[i];
        if (sceneOption.value === sceneCode) {
          return sceneOption.label
        }
      }
      return sceneCode;
    },
    /**
     * TODO 下线需要进行判断
     *
     * @param row row
     */
    toDisableOrEnable(row) {
      let vm = this
      let clone = vm.$cloneObj(row);
      clone.isDelete = clone.isDelete ^ 1
      vm.$confirm('是否确认修改为【' + (clone.isDelete === 1 ? '无效' : '有效') + '】', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$refs.strategyForm.doSave(clone);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });
    },
    /**
     * TODO 删除一条记录比较复杂
     *      暂时不支持删除
     * @param row row
     */
    deleteOne(row) {

    },
  }
}
</script>
